/*
 Copyright (C) 2003-2013 Ronald C Beavis, all rights reserved
 X! tandem 
 This software is a component of the X! proteomics software
 development project

Use of this software governed by the Artistic license, as reproduced here:

The Artistic License for all X! software, binaries and documentation

Preamble
The intent of this document is to state the conditions under which a
Package may be copied, such that the Copyright Holder maintains some 
semblance of artistic control over the development of the package, 
while giving the users of the package the right to use and distribute 
the Package in a more-or-less customary fashion, plus the right to 
make reasonable modifications. 

Definitions
"Package" refers to the collection of files distributed by the Copyright 
	Holder, and derivatives of that collection of files created through 
	textual modification. 

"Standard Version" refers to such a Package if it has not been modified, 
	or has been modified in accordance with the wishes of the Copyright 
	Holder as specified below. 

"Copyright Holder" is whoever is named in the copyright or copyrights 
	for the package. 

"You" is you, if you're thinking about copying or distributing this Package. 

"Reasonable copying fee" is whatever you can justify on the basis of 
	media cost, duplication charges, time of people involved, and so on. 
	(You will not be required to justify it to the Copyright Holder, but 
	only to the computing community at large as a market that must bear 
	the fee.) 

"Freely Available" means that no fee is charged for the item itself, 
	though there may be fees involved in handling the item. It also means 
	that recipients of the item may redistribute it under the same
	conditions they received it. 

1. You may make and give away verbatim copies of the source form of the 
Standard Version of this Package without restriction, provided that 
you duplicate all of the original copyright notices and associated 
disclaimers. 

2. You may apply bug fixes, portability fixes and other modifications 
derived from the Public Domain or from the Copyright Holder. A 
Package modified in such a way shall still be considered the Standard 
Version. 

3. You may otherwise modify your copy of this Package in any way, provided 
that you insert a prominent notice in each changed file stating how and 
when you changed that file, and provided that you do at least ONE of the 
following: 

a.	place your modifications in the Public Domain or otherwise make them 
	Freely Available, such as by posting said modifications to Usenet 
	or an equivalent medium, or placing the modifications on a major 
	archive site such as uunet.uu.net, or by allowing the Copyright Holder 
	to include your modifications in the Standard Version of the Package. 
b.	use the modified Package only within your corporation or organization. 
c.	rename any non-standard executables so the names do not conflict 
	with standard executables, which must also be provided, and provide 
	a separate manual page for each non-standard executable that clearly 
	documents how it differs from the Standard Version. 
d.	make other distribution arrangements with the Copyright Holder. 

4. You may distribute the programs of this Package in object code or 
executable form, provided that you do at least ONE of the following: 

a.	distribute a Standard Version of the executables and library files, 
	together with instructions (in the manual page or equivalent) on 
	where to get the Standard Version. 
b.	accompany the distribution with the machine-readable source of the 
	Package with your modifications. 
c.	give non-standard executables non-standard names, and clearly 
	document the differences in manual pages (or equivalent), together 
	with instructions on where to get the Standard Version. 
d.	make other distribution arrangements with the Copyright Holder. 

5. You may charge a reasonable copying fee for any distribution of 
this Package. You may charge any fee you choose for support of 
this Package. You may not charge a fee for this Package itself. 
However, you may distribute this Package in aggregate with other 
(possibly commercial) programs as part of a larger (possibly 
commercial) software distribution provided that you do not a
dvertise this Package as a product of your own. You may embed this 
Package's interpreter within an executable of yours (by linking); 
this shall be construed as a mere form of aggregation, provided that 
the complete Standard Version of the interpreter is so embedded. 

6. The scripts and library files supplied as input to or produced as 
output from the programs of this Package do not automatically fall 
under the copyright of this Package, but belong to whomever generated 
them, and may be sold commercially, and may be aggregated with this 
Package. If such scripts or library files are aggregated with this 
Package via the so-called "undump" or "unexec" methods of producing 
a binary executable image, then distribution of such an image shall 
neither be construed as a distribution of this Package nor shall it 
fall under the restrictions of Paragraphs 3 and 4, provided that you 
do not represent such an executable image as a Standard Version of 
this Package. 

7. C subroutines (or comparably compiled subroutines in other languages) 
supplied by you and linked into this Package in order to emulate 
subroutines and variables of the language defined by this Package 
shall not be considered part of this Package, but are the equivalent 
of input as in Paragraph 6, provided these subroutines do not change 
the language in any way that would cause it to fail the regression 
tests for the language. 

8. Aggregation of this Package with a commercial distribution is always 
permitted provided that the use of this Package is embedded; that is, 
when no overt attempt is made to make this Package's interfaces visible 
to the end user of the commercial distribution. Such use shall not be 
construed as a distribution of this Package. 

9. The name of the Copyright Holder may not be used to endorse or promote 
products derived from this software without specific prior written permission. 

10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 

The End 
*/

// File version: 2013-04-01

/*
	tandem.cpp provides a command line interface to the main processing class, mprocess.
	A single command line parameter is accepted, which is the file name for an XML class
	containing the parameters for performing the protein modelling session. The input file 
	also contains the name of an output file, which will contain the output from the session.
*/

#include "stdafx.h"
#include <sys/timeb.h>
#include <sys/time.h>
#include <ctime>
#include <algorithm>

#include "msequence.h"
#include "msequencecollection.h"
#include "msequenceserver.h"
#include "msequtilities.h"
#include "mspectrum.h"
#include "xmlparameter.h"
#include "mscore.h"
#include "mprocess.h"
#include <time.h>

#ifndef X_P3

/*
 * windows.h and the definition of ProcessThread are necessary for multithreading
 * in the Windows 32 environment. UNIX platforms that use POSIX threads use an
 * alternate version of this file.
 */
#include <pthread.h>
void* ProcessThread(void *pParam);
void* RefineThread(void *pParam);
#ifdef PROTEIN_GROUP
void* thread(void *vargp);
#endif


bool lessThanSpec(const mspectrum &_l,const mspectrum &_r);

bool lessThanSpec(const mspectrum &_l,const mspectrum &_r)
{
	return _l.m_dMH < _r.m_dMH;
}
bool lessThanSequenceDebugUid(const msequence &_l,const msequence &_r)
{
	return _l.m_tUid < _r.m_tUid;
}

#ifdef PROTEIN_GROUP
/*----------------------------------------------------------*/
mprocess **pProcess = NULL;
vector<msequenceCollection> mseqCol;
unsigned long libVecSize = 0;
int pProCnt = 0;
pthread_mutex_t lock_statistics = PTHREAD_MUTEX_INITIALIZER; 


pthread_mutex_t lock_crc = PTHREAD_MUTEX_INITIALIZER; 
pthread_mutex_t lock_dup = PTHREAD_MUTEX_INITIALIZER; 
pthread_mutex_t lock_arr = PTHREAD_MUTEX_INITIALIZER; 
//pthread_mutex_t lock_statistic_read; 
//pthread_mutex_t lock_protein_cnt; 


bool process_end = false;

size_t ProteinGlobalId = 0;
size_t ProteinScoredCount = 0;
static volatile int cnt = 0;
float crc_timeCnt = 0;

pthread_barrier_t bar_mpro;

unsigned int Protein_threads = 0;
unsigned int Spectra_divisions = 0;
pthread_t *tid = NULL;
class sequenceControl{
public:
	pthread_mutex_t mutex;
	pthread_cond_t cond;
	int   flag;
//	int    score_times;
	float  cost;
//	size_t lib_size;
//	size_t spe_size;
	sequenceControl(void){
		flag = -1;
		pthread_mutex_init(&mutex,NULL);
		pthread_cond_init(&cond,NULL);
		cost = 0.0;
//		score_times = 0;
//		lib_size = 0;
//		spe_size = 0;
	}
	virtual ~sequenceControl(void){
		pthread_cond_destroy(&cond);
		pthread_mutex_destroy(&mutex);
	}
};
sequenceControl *table = NULL;
/*----------------------------------------------------------*/
#endif

int main(int argc, char* argv[])
{
	/*
	* Check the argv array for at least one parameter.
	* mprocess checks the validity of the file.
	*/
	if(argc < 2 || argc > 1 && strstr(argv[1],"-L") == argv[1] || argc > 1 && strstr(argv[1],"-h") == argv[1])	{
		cout << "\n\nUSAGE: tandem filename\n\nwhere filename is any valid path to an XML input file.\n\n+-+-+-+-+-+-+\n";
		cout << "\nX! TANDEM " << VERSION << "\n";
		cout << "\nCopyright (C) 2003-2014 Ronald C Beavis, all rights reserved\n";
		cout << "This software is a component of the GPM  project.\n";
		cout << "Use of this software governed by the Artistic license.\n";
		cout << "If you do not have this license, you can get a copy at\n";
		cout << "http://www.perl.com/pub/a/language/misc/Artistic.html\n";
		cout << "\n+-+-+-+-+-+-+\n\npress <Enter> to continue ...";
		char *pValue = new char[128];
		cin.getline(pValue,127);
		delete pValue;
		return -1;
	}
	cout << "\nX! TANDEM " << VERSION << "\n\n";
	/*
	* Create an mprocess object array
	*/
	unsigned long lMaxThreads = 256;
#ifdef PROTEIN_GROUP
	pProcess = new mprocess*[lMaxThreads];
	mseqCol.reserve(1000000);
#else
	mprocess **pProcess = new mprocess*[lMaxThreads];//生成256个类，数组
#endif
	if(pProcess == NULL)	{
		cout << "An error was detected creating the processing objects.\nPlease contact a GPM administrator.\n";
		return -2;
	}
#ifdef MSVC
//	DWORD *pId = new DWORD[lMaxThreads];
//	HANDLE *pHandle = new HANDLE[lMaxThreads];
#else
//	int *pId = new int[lMaxThreads];
//	int *pHandle = new int[lMaxThreads];
	pthread_t pThreads[lMaxThreads];
#endif

	unsigned long a = 0;
	while(a < lMaxThreads)	{
		pProcess[a] = NULL;
//		pHandle[a] = NULL;
//		pId[a] = NULL;
		a++;
	}
	pProcess[0] = new mprocess;
#ifdef PROTEIN_GROUP
	pProCnt++;
#endif
	cout << "Loading spectra" << endl;

	/*
	* Initialize the first mprocess object with the input file name.
	*/
	time_t load_start = clock();
	char *pS = new char[1024];
	strcpy(pS,argv[1]);
	if(!pProcess[0]->load(pS))	{
		cout << "\n\nAn error was detected while loading the input parameters.\nPlease follow the advice above or contact a GPM administrator to help you.";
		delete pProcess[0];
		delete pProcess;
		return -4;
	}
	cout << " loaded.\n";
	if(pProcess[0]->m_vSpectra.size() == 0){
		cout << "No input spectra met the acceptance criteria.\n";
		cout.flush();
		delete pProcess[0];
		delete pProcess;
		return 1;
	}
	pProcess[0]->serialize();
	cout << "Spectra matching criteria = " << (unsigned long)pProcess[0]->m_vSpectra.size() << "\n";
	time_t load_finish = clock();
	double load_duration = (double)(load_finish - load_start)/CLOCKS_PER_SEC;
	cout<<"load duration "<<load_duration<<" seconds"<<endl;
#ifdef PLUGGABLE_SCORING
	cout << "Pluggable scoring enabled." << endl;
#endif
      /*
       * Start the mprocess object and wait for it to return.
       */
	unsigned long lThread = pProcess[0]->get_thread();
	unsigned long lThreads = pProcess[0]->get_threads();
	printf("thread:%d, %d",lThread,lThreads);
	if(lThreads	> lMaxThreads){
		lThreads = lMaxThreads;
	}
	if(pProcess[0]->m_vSpectra.size() <	lThreads){
		lThreads = (unsigned long)pProcess[0]->m_vSpectra.size();
		if(lThreads	< 1)	{
			lThreads = 1;
		}
		pProcess[0]->set_threads(lThreads);
	}
#ifdef MSVC
	DWORD dCount = lThreads	- 1;
#else
	int dCount = lThreads -	1;
#endif
	long lSpectra =	lThreads + (long)pProcess[0]->m_vSpectra.size()/lThreads;
	bool bSpectra =	true;
	cout << "Starting threads ." << endl;
//	cout.flush();

	size_t tCount = pProcess[0]->m_vSpectra.size();
	sort(pProcess[0]->m_vSpectra.begin(),pProcess[0]->m_vSpectra.end(),lessThanSpec);//template sort
	float fMax = (float)pProcess[0]->m_vSpectra.back().m_dMH;
	float fZ = pProcess[0]->m_vSpectra.back().m_fZ;
	pProcess[0]->m_fMaxMass = fMax;
	pProcess[0]->m_fMaxZ = fZ;

	if(lThread != 0xFFFFFFFF)		{ 
		/********************************
		 *  task paratitioning
		 ********************************/ 
		while(dCount > 0)		{
			pProcess[dCount] = new mprocess;
			pProcess[dCount]->set_thread(dCount);						 
			pProcess[dCount]->m_vSpectra.reserve(lSpectra);
			pProcess[dCount]->m_fMaxMass = fMax;
			pProcess[dCount]->m_fMaxZ = fZ;
			dCount--;
		}
		size_t tProcesses = lThreads;
		size_t tRing = 0;
		vector<mspectrum> vZero;
		vZero.reserve(lSpectra);
		do	{
			if(tRing == 0)	{
				vZero.push_back(pProcess[0]->m_vSpectra.back());
			}
			else	{
				pProcess[tRing]->m_vSpectra.push_back(pProcess[0]->m_vSpectra.back());
			}
			tRing++;
			pProcess[0]->m_vSpectra.pop_back();
			if(tRing == tProcesses)	{
				tRing = 0;
			}
		}	while(pProcess[0]->m_vSpectra.size() != 0);
		pProcess[0]->m_vSpectra.reserve(vZero.size());
		do	{
			pProcess[0]->m_vSpectra.push_back(vZero.back());
			vZero.pop_back();
		}	while(vZero.size() != 0);
		dCount = lThreads - 1;
		while(dCount > 0)		{
			if(!pProcess[dCount]->load(pS,pProcess[0]))	{
				cout <<	"error pProcess->LoadParameters	returned error (main)\r\n";
				delete pProcess;
				return -4;
			}
			dCount--;
			cout <<	".";
			cout.flush();
		}
	}
	delete pS;
	dCount = 0;
#ifdef PTANDEM

/*	pthread_mutex_init(&lock_statistics,NULL);
	pthread_mutex_init(&lock_clean,NULL);
	pthread_mutex_init(&lock_cnt,NULL);
	pthread_mutex_init(&lock_map,NULL);
	pthread_mutex_init(&lock_statistic_read,NULL);
	pthread_mutex_init(&lock_protein_cnt,NULL);
	pthread_mutex_init(&lock_debug,NULL);
	pthread_barrier_init(&bar_mpro,NULL,Threads_Max + 1);
*/
	struct timeval proc_start, proc_end;
	gettimeofday(&proc_start,NULL);

	ProcessThread((void*)pProcess[dCount]); // main thread

	string strValue, strKey;
	strKey = "spectrum, divisions";
	pProcess[0]->m_xmlValues.get(strKey,strValue);
	Spectra_divisions = atoi(strValue.c_str());

	strKey = "threads";
	pProcess[0]->m_xmlValues.get(strKey,strValue);
	Protein_threads = atoi(strValue.c_str());

	tid = new pthread_t[Protein_threads];
	table = new sequenceControl[Protein_threads];

//	std::reverse(mseqCol.begin(),mseqCol.end());
	libVecSize = mseqCol.size();
	
	for(int i = 0; i < Protein_threads-1;++i){
		cout << table[i].flag << endl;
	}
	/* make sequential table
	for(int i = 0; i < Protein_threads; ++i){
		table[i] = new sequenceControl[Protein_threads + Spectra_divisions -1]();
		for(int j = 0; j < Spectra_divisions; ++j){
			table[i][j+i].flag = true;
		}
	}
	for(int i = 0; i < Protein_threads; ++i){
		for(int j = 0; j < Spectra_divisions + Protein_threads - 1; ++j){
			cout << table[i][j].flag << " ";
		}
		cout << endl;
	}
	 * */

	for(int i = 0; i < Protein_threads; ++i){
		pProcess[pProCnt] = new mprocess;
		pthread_create(&tid[i],NULL,thread,(void*)pProCnt);
		pProCnt++;
	}

#endif

	dCount++;

	/*
	* Initialize more mprocess objects, if lThread is not 0xFFFFFFFF, which signifies default single
	* threaded operation.
	*/

	cout << "started.\n";
	cout << "Computing models:" << endl;
	/*
	* wait until all of the mprocess objects return.
	*/
	for(int i = 0; i < Protein_threads; ++i){
		pthread_join(tid[i],NULL);
	}

	for(int i = 0; i < Protein_threads-1;++i){
		cout << table[i].flag << endl;
	}
/*
	for(int i = 0; i < Protein_threads; ++i){
		for(int j = 0; j < Spectra_divisions + Protein_threads - 1; ++j){
			printf("%d , ",table[i][j].flag);
		}
		printf("\n");
	}
	cout << "-------flag-------" << endl;
	
	for(int i = 0; i < Protein_threads; ++i){
		for(int j = 0; j < Spectra_divisions + Protein_threads - 1; ++j){
			printf("%d , ",table[i][j].score_times);
		}
		printf("\n");
	}
	cout << "-----score-times-----" << endl;
	for(int i = 0; i < Protein_threads; ++i){
		for(int j = 0; j < Spectra_divisions + Protein_threads - 1; ++j){
			printf("%d , ",table[i][j].spe_size);
		}
		printf("\n");
	}
	cout << "-----libe-size--------" << endl;
	for(int i = 0; i < Protein_threads; ++i){
		for(int j = 0; j < Spectra_divisions + Protein_threads - 1; ++j){
			printf("%.6lf , ",table[i][j].cost);
		}
		printf("\n");
	}
	cout << "-----cost-------------" << endl;
*/
	pProcess[0]->clean_sequences();
//	cout << "\tsequences modelled = "<< (long)(pProcess[0]->get_protein_count()/1000.0 + 0.5) << " ks\n";
	cout << "\tsequences modelled = "<< (long)(pProcess[0]->get_protein_count()) << endl;

#if 0
	ofstream ccout("bottelneck-analysis.csv",ios::out | ios::app);
//	ccout << "buf remove time , " << pProcess[0]->buf_t.buf_Inserttime_cnt<<endl;
	ccout << "buf remove time , " << pProcess[0]->buf_t.buf_Removetime_cnt / 1000000.0<<endl;
	float avg = 0.0;
	for(size_t i = 0; i < pProCnt; ++i){
		ccout << "count time  dups " << i << "," << pProcess[i]->crc_time_thread / 1000000.0 << endl; 
		avg += pProcess[i]->crc_time_thread / 1000000.0 ;
	}
	ccout << "thread count time  dups " << "," << (avg/pProCnt) << endl; 
	avg = 0;
	for(size_t i = 0; i < pProCnt; ++i){
		ccout <<"statistical add time " << i << "," << pProcess[i]->statistical_add_time_thread << endl; 
		avg +=  pProcess[i]->statistical_add_time_thread; 
	}
	ccout <<"thread statistical add time " << "," <<(avg/pProCnt) << endl; 
	avg = 0;
	for(size_t i = 0; i < pProCnt; ++i){
		ccout <<"statistical total time "<< i <<","<< pProcess[i]->statistical_total_time_thread / 1000000.0<< endl; 
		avg += pProcess[i]->statistical_total_time_thread / 1000000.0; 
	}
	ccout <<"statistical total time "<< ","<< (avg/pProCnt) << endl; 
	/*statistic from mprocess*/
	float fHyper = 0.0;//record highest score
	const float EPSINON = 1e-6;
	vector<mspectrum> tmp_Spectra;
	//tmp_Spectra.clear();
	mspectrum tmp_Spectrum;
	for(size_t i = 0;i < pProcess[0]->m_vSpectra.size();++i){
		tmp_Spectrum = pProcess[0]->m_vSpectra[i];
		tmp_Spectra.push_back(tmp_Spectrum);
	}
	for(size_t i  = 0; i < tmp_Spectra.size();++i){//设置tmp的每个m_vseqBest
		for(size_t j = 1; j < pProCnt;++j){//从每个class中当前这个质谱对应的m_vseqBest统计
			if(fHyper < pProcess[j]->m_vSpectra[i].m_fHyper){ //清空本m_vseqBest
				fHyper = pProcess[j]->m_vSpectra[i].m_fHyper;
				tmp_Spectra[i].m_fHyper = fHyper;
				tmp_Spectra[i].m_vseqBest.clear();
				for(size_t t = 0; t < pProcess[j]->m_vSpectra[i].m_vseqBest.size();++t){
					tmp_Spectra[i].m_vseqBest.push_back(pProcess[j]->m_vSpectra[i].m_vseqBest[t]);
				}
			} else if(abs(fHyper - pProcess[j]->m_vSpectra[i].m_fHyper) <= EPSINON){//并列保存到m_vseqBest
				for(size_t t=0;t < pProcess[j]->m_vSpectra[i].m_vseqBest.size();++t){
					tmp_Spectra[i].m_vseqBest.push_back(pProcess[j]->m_vSpectra[i].m_vseqBest[t]);
				}
			}
		}
		pProcess[0]->m_vSpectra[i].m_vseqBest.clear();
		for(size_t tt = 0; tt < tmp_Spectra[i].m_vseqBest.size();++tt){
			pProcess[0]->m_vSpectra[i].m_vseqBest.push_back(tmp_Spectra[i].m_vseqBest[tt]);
		}
	}

/*
	sort(pProcess[0]->m_debug.begin(),pProcess[0]->m_debug.end(),lessThanSequenceDebugUid);
	ofstream trace("debug-mdomain.csv");
	for(const auto &_s : pProcess[0]->m_debug){
		trace << _s.m_tUid << "	";
		for(const auto &_m : _s.m_vDomains){
			trace<<"<"<<_m.m_lS<<"~"<<_m.m_lE<<","<<_m.m_dMH<<","<<_m.m_fScore<<"~"<<_m.m_fHyper<<","<<_m.m_lEqualsS<<" ";
			for(size_t i = 0; i < _m.m_lEqualsS; ++i){
				trace << "{" << _m.tId[i] << "," << _m.fScore[i] << "," << _m.fHyper[i] << "}";
			}
			trace << ">";
		}
		trace << endl;
	}!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
#endif
	gettimeofday(&proc_end,NULL);
	ofstream fout;
	fout.open("record-mutex-perf.csv",ios::out | ios::app);
	float process_duration = (proc_end.tv_sec - proc_start.tv_sec) + 
						(proc_end.tv_usec - proc_start.tv_usec) / 1000000.0; 
	fout << "ptandem spend  , "<< process_duration << ", ptandem thread , "<< Protein_threads <<endl;
	cout << "ptandem spend  , "<< process_duration << ", ptandem thread , "<< Protein_threads <<endl;

	pProcess[0]->merge_spectra();

	pProcess[0]->m_pScore->set_mtA_filter(0,pProcess[0]->m_vSpectra.size());
	cout << "************************************" << endl;
//	cout << "mprocess[0] m_debug size : " << pProcess[0]->m_debug.size() << endl;
//	cout << "thread  tLength  cnt     : " << cnt << endl;
//	cout << "Protein_Scored_Count     : " << ProteinScoredCount<< endl;
//	cout << "Protein Global Id Max    : " << ProteinGlobalId << endl;
	cout << " m_vseqBest   size       : " << pProcess[0]->m_vseqBest.size() << endl;
	cout << "************************************" << endl;
	cout << "************************************" << endl;
//	cout << " m_tTotalResidues size   : " << pProcess[0]->m_tTotalResidues << endl;
//	cout << " m_tPeptideCount  size   : " << pProcess[0]->m_tPeptideCount << endl;
//	cout << " m_tTotalResidues size   : " << pProcess[0]->m_tTotalResidues << endl;
	cout << "************************************" << endl;

/*
	ofstream out("PPI-seqBest-dump.csv");
	for(size_t i = 0; i < pProcess[0]->m_vseqBest.size(); ++i){
		out <<"m_iRound,"<<pProcess[0]->m_vseqBest[i].m_iRound
	//		<<",m_bForward,"<<pProcess[0]->m_vseqBest[i].m_bForward
			<<",m_tUid,"<<pProcess[0]->m_vseqBest[i].m_tUid
	//		<<",m_fScore,"<<pProcess[0]->m_vseqBest[i].m_fScore
	//		<<",m_fHyper,"<<pProcess[0]->m_vseqBest[i].m_fHyper
	//		<<",m_dExpect,"<<pProcess[0]->m_vseqBest[i].m_dExpect
	//		<<",m_fIntensity,"<<pProcess[0]->m_vseqBest[i].m_fIntensity
			<<",m_strDes,"<<pProcess[0]->m_vseqBest[i].m_strDes
	//		<<",m_strSeq,"<<pProcess[0]->m_vseqBest[i].m_strSeq
			<<",m_siPath,"<<pProcess[0]->m_vseqBest[i].m_siPath << endl;
	}
	cout.flush();

	ofstream sout("mapSequences.csv");
	sout << "m_tUid,string" << endl;
	size_t i = 0;
	for(const auto &w : pProcess[0]->m_mapSequences){
		sout << w.first << "," << w.second << endl;
		i++;
	}
	cout << "pProcess[0]->m_mapSequences size : " << i << endl;
*/
#if 10
	a = 1;
	/*
	* merge the results into the first object
	*/
	while(a < dCount)	{
		pProcess[0]->merge_map(pProcess[a]->m_mapSequences);
		pProcess[0]->merge_spectra(pProcess[a]->m_vSpectra);
		a++;
	}
	a = 1;
	pProcess[0]->load_sequences();
	while(a < dCount)	{
		pProcess[a]->merge_map(pProcess[0]->m_mapSequences);
		pProcess[a]->m_vseqBest = pProcess[0]->m_vseqBest;
		pProcess[a]->m_mapCrc.clear();
		a++;
	}
	/*
	* Report the contents of the mprocess objects into an XML file as described
	* in the input file.
	*/
	cout << "Model refinement:\n";
	time_t refine_start = clock();
	cout.flush();

	dCount = 0;
//#ifdef MSVC
//	pHandle[dCount] = CreateThread(NULL,0,RefineThread,(void *)pProcess[dCount],0,&pId[dCount]);
//#else
//#ifdef PTANDEM
	RefineThread((void*)pProcess[dCount]);
//#else
//	pthread_create(&pThreads[dCount],NULL,RefineThread,(void*)pProcess[dCount]);
//#endif
//#endif
	dCount++;
	/*
	* Initialize more mprocess objects, if lThread is not 0xFFFFFFFF, which signifies default single
	* threaded operation.
	*/
	if(lThread != 0xFFFFFFFF)	{
		while(dCount < lThreads)	{
//#ifdef MSVC
//			pHandle[dCount] = CreateThread(NULL,0,RefineThread,(void *)pProcess[dCount],0,&pId[dCount]);
//#else
			pthread_create(&pThreads[dCount],NULL,RefineThread,(void*)pProcess[dCount]);
//#endif
			dCount++;
		}
	}
	/*
	* wait until all of the mprocess objects return.
	*/
//#ifdef MSVC
//	wait = WaitForMultipleObjects(dCount,pHandle,true,INFINITE);
//#else
	//2003-03-01:note - the declaration below was changed from void **vp;	
//	x=0;
//	for(x=0;x<dCount;x++){
		//2003-03-01:note - the 2nd parameter in the call to pthread_join() was changed from vp
//#ifndef PTANDEM
//		wait = pthread_join(pThreads[x],&vp);
//#endif
//	}
//#endif
	a = 1;
	/*
	* merge the results into the first object
	*/
	if(dCount > 1)	{
		cout << "Merging results:\n";
		cout.flush();
	}
	while(a < dCount)	{
		if(a == 1)	{
			cout << "\tfrom " << a+1;
		}
		else	{
			cout << a+1;
		}
		cout.flush();
		if(!pProcess[0]->add_spectra(pProcess[a]->m_vSpectra))	{
			cout << "adding spectra failed.\n";
		}
		pProcess[0]->merge_statistics(pProcess[a]);
		pProcess[a]->clear();
		pProcess[a]->m_mapSequences.clear();
		delete pProcess[a];
		pProcess[a] = NULL;
		a++;
	}
	if(dCount > 1)	{
		cout << "\n\n";
		cout.flush();
	}
	cout.flush();
	time_t refine_finish = clock();
	double refine_duration = (double)(refine_finish - refine_start)/CLOCKS_PER_SEC;
	cout<<"refine duration "<<refine_duration<<" seconds"<<endl;
	cout << "Creating report:\n";
	time_t report_start = clock();
	cout.flush();
	pProcess[0]->report();
	size_t tValid = pProcess[0]->get_valid();
	size_t tUnique = pProcess[0]->get_unique();
	double dE = pProcess[0]->get_error_estimate();
	unsigned long lE = (unsigned long)(0.5+dE);
	unsigned long lEe = (unsigned long)(0.5 + sqrt(dE));
	if(lEe == 0)	{
		lEe = 1;
	}
	if(dE <= 0.0)	{
		dE = 1.0;
	}
	cout << "\nValid models = " << (unsigned long)tValid << "\n";
	if(tUnique > 0)	{
		cout << "Unique models = " << (unsigned long)tUnique << "\n";
		cout << "Estimated false positives = " << lE << " &#177; ";
		cout << lEe << "\n";
	}
	lE = pProcess[0]->get_reversed();
	if(lE != -1)	{
		cout << "False positive rate (reversed sequences) = " << lE << "\n";
	}

	time_t report_finish = clock();
	double report_duration = (double)(report_finish - report_start)/CLOCKS_PER_SEC;
	cout << "report duration "<<report_duration<<" seconds"<<endl;
	cout << "\n\n";

	ofstream pout("Protein_cnt.csv");
	for(const auto _p : pProcess[0]->protein_cnt){
		pout << _p.first << ", " << _p.second << endl;
	}
	/*
	* Delete the mprocess objects and exit
	*/
	a = 0;
	while(a < 16)	{
		if(pProcess[a] != NULL)	{
#ifdef MSVC
//			CloseHandle(pHandle[a]);
#endif
//			delete pProcess[a];
		}
		a++;
	}
#endif
	delete pProcess;
//	delete pId;
//	delete pHandle;
	
	return 0;

}
/*
 * Process thread is used to create the worker threads for each mprocess object
 */

#ifdef MSVC
DWORD WINAPI ProcessThread(LPVOID _p)
{
	((mprocess *)_p)->process();
	return 0;
}
DWORD WINAPI RefineThread(LPVOID _p)
{
	((mprocess *)_p)->refine();
	return 0;
}
#else
void* ProcessThread(void *_p){
	((mprocess *)_p)->process();
	return (void*)0;
}
void* RefineThread(void *_p){
	((mprocess *)_p)->refine();
 	return (void*)0;
}

#ifdef PROTEIN_GROUP
void* thread(void *_p){
/*
 *
 * */
//	mprocess *tmp = (mprocess *)_p;// just pointer copy
	long pro_ID = ((long )_p);
	mprocess *tmp = pProcess[0];
	mprocess *tpro = pProcess[pro_ID];
	msequenceCollection item;
	bool endFlag = true;
	
//	pthread_barrier_wait(&bar_mpro);
	//synchronization
#if 10
	tpro->m_vstrPaths.clear();
	size_t a = 0;
	while(a < tmp->m_vstrPaths.size()){
		tpro->m_vstrPaths.push_back(tmp->m_vstrPaths[a]);
		a++;
	}
	tpro->m_xmlPerformance = tmp->m_xmlPerformance; //
	tpro->m_xmlValues = tmp->m_xmlValues; // 必须等到main thread在把参数配置好
	a = 0;
	tpro->m_vSpectra.clear();
	while(a < tmp->m_vSpectra.size()){
		tpro->m_vSpectra.push_back(tmp->m_vSpectra[a]);
		a++;
	}
	tpro->m_mapSequences.clear();
	if(tmp->m_mapSequences.size() > 0){
		tpro->m_mapSequences = tmp->m_mapSequences;
	}
	//tpro->m_vseqBest = tmp->m_vseqBest;
	a = 0;
	while(a < tmp->m_vstrModifications.size()){
		tpro->m_vstrModifications.push_back(tmp->m_vstrModifications[a]);
		a++;
	}
		/*
		 * refine parameter , refine stage dynamic set
		 * */
	a = 0;
	tpro->m_viQuality.clear();
	while(a < tmp->m_viQuality.size()){
		tpro->m_viQuality.push_back(tmp->m_viQuality[a]);
		a++;
	}
	tpro->m_mapCrc.clear();
	if(tmp->m_mapCrc.size() > 0){
		tpro->m_mapCrc = tmp->m_mapCrc;
	}
	tpro->m_tTemp = tmp->m_tTemp;
	tpro->m_bReversedOnly = tmp->m_bReversedOnly;
	tpro->m_bSaps = tmp->m_bSaps;
	tpro->m_bAnnotation = tmp->m_bAnnotation;
	tpro->m_bMinimalAnnotation = tmp->m_bMinimalAnnotation;
	tpro->m_bSerialize = tmp->m_bSerialize;
	tpro->m_bCheckNg = tmp->m_bCheckNg;
	tpro->m_bSkyline = tmp->m_bSkyline;
	tpro->m_bUseCrc = tmp->m_bUseCrc;
	tpro->m_strSkyline = tmp->m_strSkyline;
	tpro->m_dNt = tmp->m_dNt;
	tpro->m_dNtAve = tmp->m_dNtAve;
	tpro->m_dNg = tmp->m_dNg;
	tpro->m_dNgAve = tmp->m_dNgAve;
	tpro->m_fMaxMass = tmp->m_fMaxMass;
	tpro->m_fMaxZ = tmp->m_fMaxZ;

	tpro->m_strLastMods = tmp->m_strLastMods;
	tpro->m_iCurrentRound = tmp->m_iCurrentRound;
	tpro->m_bPermute = tmp->m_bPermute;
	tpro->m_bPermuteHigh = tmp->m_bPermuteHigh;
	tpro->m_bCrcCheck = tmp->m_bCrcCheck;
	tpro->m_bQuickAcetyl = tmp->m_bQuickAcetyl;
	tpro->m_bQuickPyro = tmp->m_bQuickPyro;
	tpro->m_dEsum = tmp->m_dEsum;
	tpro->m_bChargeDither = tmp->m_bChargeDither;
	tpro->m_fSavResolve = tmp->m_fSavResolve;
	tpro->m_bSavResolve = tmp->m_bSavResolve;
	tpro->m_setRound.clear();
	if(tmp->m_setRound.size() > 0){
		tpro->m_setRound = tmp->m_setRound;
	}
	a = 0;
	tpro->m_vstrSaps.clear();
	while(a < tmp->m_vstrSaps.size()){
		tpro->m_vstrSaps.push_back(tmp->m_vstrSaps[a]);
		a++;
	}
	a = 0;
	tpro->m_vstrMods.clear();
	while(a < tmp->m_vstrMods.size()){
		tpro->m_vstrMods.push_back(tmp->m_vstrMods[a]);
		a++;
	}
	tpro->m_mapAnnotation.clear();
	if(tmp->m_mapAnnotation.size() > 0){
		tpro->m_mapAnnotation = tmp->m_mapAnnotation;
	}
	tpro->m_pmapAnnotation = &(tpro->m_mapAnnotation);

	tpro->m_semiState = tmp->m_semiState;
	tpro->m_pyroState = tmp->m_pyroState;
	tpro->m_errValues = tmp->m_errValues;

	tpro->m_dSearchTime = tmp->m_dSearchTime;
	tpro->m_lIonCount = tmp->m_lIonCount;

	tpro->m_lReversed = tmp->m_lReversed;
	tpro->m_dThreshold = tmp->m_dThreshold;
	tpro->m_tContrasted = tmp->m_tContrasted;
	tpro->m_lStartMax = tmp->m_lStartMax;
	tpro->m_lCStartMax = tmp->m_lCStartMax;
	
	if(tmp->m_pSeq == NULL){
		tpro->m_pSeq = NULL;
	} else {
		strcpy(tpro->m_pSeq,tmp->m_pSeq);
	}
	tpro->m_bUn = tmp->m_bUn;
	tpro->m_bUseHomologManagement = tmp->m_bUseHomologManagement;
	tpro->m_tMinResidues = tmp->m_tMinResidues;
	tpro->m_tMaxResidues = tmp->m_tMaxResidues;
	tpro->m_tMissedCleaves = tmp->m_tMissedCleaves;
	tpro->m_tSpectra = tmp->m_tSpectra;
	tpro->m_tSpectraTotal = tmp->m_tSpectraTotal;
	tpro->m_tValid = tmp->m_tValid;
	tpro->m_tTotalResidues = tmp->m_tTotalResidues;
	tpro->m_tSeqSize = tmp->m_tSeqSize;
	tpro->m_tUnique = tmp->m_tUnique;
	tpro->m_strOutputPath = tmp->m_strOutputPath;

	tpro->m_Cleave = tmp->m_Cleave;
	tpro->m_seqCurrent = tmp->m_seqCurrent;

	tpro->m_svrSequences = tmp->m_svrSequences;
	tpro->m_specCondition = tmp->m_specCondition;

	tpro->m_pScore = mscoremanager::create_mscore(tmp->m_xmlValues);
	if(tpro->m_pScore != NULL)
		tpro->m_pScore->load_param(tmp->m_xmlValues);
	
	tpro->m_pScore->m_dErr = tmp->m_pScore->m_dErr;
	tpro->m_pScore->m_tScored = tmp->m_pScore->m_tScored;
	tpro->m_pScore->m_dHomoError = tmp->m_pScore->m_dHomoError;
	//tpro->m_pScore->m_fHyper = tmp->m_pScore->m_fHyper;
	tpro->m_pScore->m_dParentErrMinus = tmp->m_pScore->m_dParentErrMinus;
	tpro->m_pScore->m_dParentErrPlus = tmp->m_pScore->m_dParentErrPlus;
	tpro->m_pScore->m_dMaxMass = tmp->m_pScore->m_dMaxMass;
	tpro->m_pScore->m_dMinMass = tmp->m_pScore->m_dMinMass;
	tpro->m_pScore->m_lMaxCharge = tmp->m_pScore->m_lMaxCharge;
	tpro->m_pScore->m_lMaxPeaks = tmp->m_pScore->m_lMaxPeaks;
	tpro->m_pScore->m_dScale = tmp->m_pScore->m_dScale;

	//tpro->m_pScore->m_seqUtil.set_seqUtil(tmp->m_pScore->m_seqUtil);
	tpro->m_pScore->m_seqUtil = tmp->m_pScore->m_seqUtil;
	//tpro->m_pScore->m_seqUtil.set_seqUtil(tmp->m_pScore->m_seqUtilAvg);
	tpro->m_pScore->m_seqUtilAvg = tmp->m_pScore->m_seqUtilAvg;
	tpro->m_pScore->m_pSeqUtilFrag = &(tpro->m_pScore->m_seqUtil);

	tpro->m_pScore->m_State = tmp->m_pScore->m_State;//????
	tpro->m_pScore->m_Pam = tmp->m_pScore->m_Pam;
	tpro->m_pScore->m_Sap = tmp->m_pScore->m_Sap;
	tpro->m_pScore->m_Term = tmp->m_pScore->m_Term;

	tpro->m_pScore->m_lType = tmp->m_pScore->m_lType;

	tpro->m_pScore->m_dWE = tmp->m_pScore->m_dWE;
	tpro->m_pScore->m_bPhosphoBias = tmp->m_pScore->m_bPhosphoBias;
	tpro->m_pScore->m_bUsePam = tmp->m_pScore->m_bUsePam;
	tpro->m_pScore->m_bUseSaps = tmp->m_pScore->m_bUseSaps;
	tpro->m_pScore->m_bIsC = tmp->m_pScore->m_bIsC;
	tpro->m_pScore->m_bIsN = tmp->m_pScore->m_bIsN;
	tpro->m_pScore->m_bIsotopeError = tmp->m_pScore->m_bIsotopeError;
	tpro->m_pScore->m_lMILength = tmp->m_pScore->m_lMILength;
	tpro->m_pScore->m_lSeqLength = tmp->m_pScore->m_lSeqLength;
	tpro->m_pScore->m_lMaxSeqLength = tmp->m_pScore->m_lMaxSeqLength;
	tpro->m_pScore->m_lMinSeqLength = tmp->m_pScore->m_lMinSeqLength;
	tpro->m_pScore->m_lSize = tmp->m_pScore->m_lSize;
	tpro->m_pScore->m_lComplexity = tmp->m_pScore->m_lComplexity;
	tpro->m_pScore->m_lSpectra = tmp->m_pScore->m_lSpectra;
	tpro->m_pScore->m_lErrorType = tmp->m_pScore->m_lErrorType;

	tpro->m_pScore->m_fScore = tmp->m_pScore->m_fScore;
	tpro->m_pScore->m_dSeqMH = tmp->m_pScore->m_dSeqMH;
	tpro->m_pScore->m_dWidth = tmp->m_pScore->m_dWidth;
	tpro->m_pScore->m_dComplexity = tmp->m_pScore->m_dComplexity;

	tpro->m_pScore->m_lDetails = tmp->m_pScore->m_lDetails;
	tpro->m_pScore->m_iCharge = tmp->m_pScore->m_iCharge;
	tpro->m_pScore->m_bMini = tmp->m_pScore->m_bMini;

	tpro->m_pScore->m_vSpec = tmp->m_pScore->m_vSpec;
	tpro->m_pScore->m_vDetails = tmp->m_pScore->m_vDetails;
	tpro->m_pScore->m_sIndex = tmp->m_pScore->m_sIndex;

//	m_pCrcTable
	tpro->m_mapDups.clear();
	if(tmp->m_mapDups.size() > 0){
		tpro->m_mapDups = tmp->m_mapDups;
	}
	tpro->m_tDuplicates = tmp->m_tDuplicates;
	tpro->m_tDuplicateIds = tmp->m_tDuplicateIds;

	tpro->m_tPeptideCount = 0;// tmp->m_tPeptideCount;
	tpro->m_tPeptideScoredCount =  0;//tmp->m_tPeptideScoredCount;
	tpro->m_tProteinCount = 0;//tmp->m_tProteinCount;
	size_t m_tSpectra = tpro->m_vSpectra.size();
	a = 0;
/*	while(a < m_tSpectra) {
		tpro->m_pScore->add_details(tpro->m_vSpectra[a]);
		a++;
	}
*/
	tpro->m_pScore->m_vSpec.clear();
	tpro->m_pScore->m_vSpec = tmp->m_pScore->m_vSpec;
	tpro->m_pScore->m_vDetails.clear();
	tpro->m_pScore->m_vDetails = tmp->m_pScore->m_vDetails;
	tpro->m_pScore->sort_details();
	a = 0;
	while(a < m_tSpectra) {
		tpro->m_pScore->add_mi(tpro->m_vSpectra[a]);
		a++;
	}
	tpro->removeMI();
	tpro->residues();
#endif

	size_t lib_start = ((pro_ID - 1) * libVecSize) / Protein_threads;
	size_t lib_end = (pro_ID * libVecSize) / Protein_threads ;
//	string s = "lib" + std::to_string(pthread_self());
//	ofstream libout(s);	libout << lib_start << ", " << lib_end << endl;	libout.close();
	unsigned int thread_i = pro_ID - 1;
	size_t Spectra_size = pProcess[0]->m_vSpectra.size();
#if 10
	struct timeval pipe_start, pipe_end;
	float time = 0.0;
//	gettimeofday(&pipe_start,NULL);
	tpro->crc_repeat = false;// keep the crc detect only once
	for(unsigned int spectra_j = 0; spectra_j < Spectra_divisions; ++spectra_j){
		size_t spec_start = (spectra_j * Spectra_size) / Spectra_divisions;
		size_t spec_end = ((spectra_j+1) * Spectra_size) / Spectra_divisions ;
		gettimeofday(&pipe_start,NULL);

		if(thread_i > 0){
			//keep order
			pthread_mutex_lock(&(table[thread_i-1].mutex));
			while(table[thread_i-1].flag < spectra_j){
				pthread_cond_wait(&(table[thread_i-1].cond),&(table[thread_i-1].mutex));
			}
			pthread_mutex_unlock(&(table[thread_i-1].mutex));
		}
//		int score_times = 0;
		//spectra flow set
		tpro->m_pScore->set_mtA_filter(spec_start,spec_end);
		//score_each_sequence
		for(size_t lib_i = lib_start; lib_i < lib_end; ++lib_i){
			(*(tpro->m_svrSequences.m_pCol)) = mseqCol[lib_i];
			//score result write in pProcess[0]->m_vSpectra directly.
			tpro->score_each_sequence();
//			score_times += tpro->m_svrSequences.m_pCol->m_tLength;
		}
		//0,1,2,n-1 thread need send signal.last one thread do not need any status
		if(thread_i < (Protein_threads-1)){
			pthread_mutex_lock(&table[thread_i].mutex);
			table[thread_i].flag =  spectra_j;
			pthread_cond_signal(&table[thread_i].cond);
			pthread_mutex_unlock(&table[thread_i].mutex);
		}
		gettimeofday(&pipe_end,NULL);
		time = (pipe_end.tv_sec-pipe_start.tv_sec)+(pipe_end.tv_usec-pipe_start.tv_usec)/1000000.0; 
		if(!tpro->crc_repeat)
			tpro->crc_repeat = true;// keep the crc detect only once
	}
#endif
	return (void*)0;
}
#endif
#endif
#endif
